using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BlendSkybox : MonoBehaviour {
	public bool showHelpUI 			= true;
	
	public float dayLength 			= 5;
	public float nightLength 		= 5;
	public float timeCycle			= 24;
	
	public float sunIntensity 		= 0.5f;
	
	public float sunSetDuration		= 10;
	public float sunRiseDuration	= 10;
	
	public Flare sunFlare;

	public Skybox skybox;

	private Material material;
	private GameObject sun;
	private float Blender = 0;
	
	private float time;
	private float timeSpeed = 1;
	
	void Start(){
		material = skybox.material;
		
		float totalLength 	= dayLength + nightLength;
		dayLength 		  	= dayLength/totalLength;
		dayLength 			*= 100;
		nightLength			= nightLength/totalLength;
		nightLength 		*= 100;
		sunRiseDuration		/= totalLength;
		sunRiseDuration		*= 100;
		sunSetDuration		/= totalLength;
		sunSetDuration		*= 100;
		
		//0 = Dawn
		time 				= 0;

        GameObject lightGameObject 			= new GameObject("The Sun");
        lightGameObject.AddComponent<Light>();
        lightGameObject.light.color 		= Color.white;
        lightGameObject.transform.position 	= new Vector3(0, 5000, 0);
		lightGameObject.light.type 			= LightType.Directional;
		lightGameObject.light.intensity 	= sunIntensity;
		lightGameObject.light.range 		= 10000;
		lightGameObject.transform.Rotate(new Vector3(90,0,0));
		lightGameObject.light.flare			= sunFlare;
		sun = lightGameObject;
	}
	
	
		void Update() {
		float sunIntensityLerpValue = 1;
		int LerpValue 				= 1;
		float skyBoxLerpValue 		= 1;
		//Dawn
		if (time <= dayLength/2 || time >= 100-dayLength/2) {
			if (time >= 100-dayLength/2){
				sunIntensityLerpValue = Mathf.Clamp(((time-dayLength/2-nightLength)/sunRiseDuration),0,1);
			}
			else 
				sunIntensityLerpValue = 1;
			LerpValue =1;
		}
		//Dusk
		else if (time > dayLength/2 && time < 100-dayLength/2){
			sunIntensityLerpValue = Mathf.Clamp(((time-dayLength/2)/(sunSetDuration)),0,1);
			LerpValue = 0;
		}
		skyBoxLerpValue = sunIntensityLerpValue;
		
		//Debug.Log("Intensity = " + sun.light.intensity.ToString() + "   Lerp = " + sunIntensityLerpValue.ToString() + "   Time = " + time.ToString());
		sun.light.intensity = Mathf.Lerp((1-LerpValue)*sunIntensity,LerpValue*sunIntensity,sunIntensityLerpValue);		
		Blender = Mathf.Lerp((LerpValue),1-LerpValue,skyBoxLerpValue);		
		
		material.SetFloat("_Blend",Blender);	
		
		time += Time.deltaTime*(100/timeCycle)*timeSpeed;
		time = (time%100);
		
		float scalar;
		float day = dayLength/100;
		float night = nightLength/100;
		scalar = 1/night;
		if (time < dayLength/2 || time > 100-(dayLength/2))
			scalar = 1/day;
		
		sun.light.transform.RotateAround(new Vector3(0,0,0),new Vector3(10,0,0),(180/timeCycle)*Time.deltaTime*timeSpeed*scalar);
	}

	
	
	void OnGUI () {
		if (showHelpUI == true){
			if (GUI.Button(new Rect(Screen.width/2-75,Screen.height-100,25,25),"-","box") && timeSpeed > 0.11){
				if (timeSpeed <= 0.5)
					timeSpeed -= 0.1f;
				if (timeSpeed > 0.5)
					timeSpeed -= 0.5f;
			}
			if (GUI.Button(new Rect(Screen.width/2+50,Screen.height-100,25,25),"+","box")){
				if (timeSpeed >= 0.5)
					timeSpeed += 0.5f;
				if (timeSpeed < 0.5)
					timeSpeed += 0.1f;
			}
			GUI.Label(new Rect(Screen.width/2-25,Screen.height-125,50,25),"x" + timeSpeed.ToString(),"box");
			GUI.Label(new Rect(Screen.width/2-50,Screen.height-100,100,25),"Time Speed","box");
		}
		
	}
	
}
